package javolution.io;

import java.io.CharConversionException;
import java.io.IOException;
import java.io.Reader;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import javolution.lang.Reusable;
import okio.Utf8;
import org.apache.commons.net.bsd.RCommandClient;

/* loaded from: classes2.dex */
public final class UTF8ByteBufferReader extends Reader implements Reusable {
    private ByteBuffer _byteBuffer;
    private int _code;
    private int _moreBytes;

    private int read2(byte b) throws IOException {
        if (b >= 0) {
            try {
                if (this._moreBytes == 0) {
                    return b;
                }
            } catch (BufferUnderflowException unused) {
                throw new CharConversionException("Incomplete Sequence");
            }
        }
        if ((b & 192) == 128 && this._moreBytes != 0) {
            this._code = (b & Utf8.REPLACEMENT_BYTE) | (this._code << 6);
            int i = this._moreBytes - 1;
            this._moreBytes = i;
            return i == 0 ? this._code : read2(this._byteBuffer.get());
        } else if ((b & 224) == 192 && this._moreBytes == 0) {
            this._code = b & 31;
            this._moreBytes = 1;
            return read2(this._byteBuffer.get());
        } else if ((b & 240) == 224 && this._moreBytes == 0) {
            this._code = b & 15;
            this._moreBytes = 2;
            return read2(this._byteBuffer.get());
        } else if ((b & 248) == 240 && this._moreBytes == 0) {
            this._code = b & 7;
            this._moreBytes = 3;
            return read2(this._byteBuffer.get());
        } else if ((b & 252) == 248 && this._moreBytes == 0) {
            this._code = b & 3;
            this._moreBytes = 4;
            return read2(this._byteBuffer.get());
        } else if ((b & 254) == 252 && this._moreBytes == 0) {
            this._code = b & 1;
            this._moreBytes = 5;
            return read2(this._byteBuffer.get());
        } else {
            throw new CharConversionException("Invalid UTF-8 Encoding");
        }
    }

    @Override // java.io.Reader, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        if (this._byteBuffer != null) {
            reset();
        }
    }

    @Override // java.io.Reader
    public int read() throws IOException {
        ByteBuffer byteBuffer = this._byteBuffer;
        if (byteBuffer != null) {
            if (byteBuffer.hasRemaining()) {
                byte b = this._byteBuffer.get();
                return b >= 0 ? b : read2(b);
            }
            return -1;
        }
        throw new IOException("Reader closed");
    }

    @Override // java.io.Reader
    public int read(char[] cArr, int i, int i2) throws IOException {
        ByteBuffer byteBuffer;
        int i3;
        ByteBuffer byteBuffer2 = this._byteBuffer;
        if (byteBuffer2 != null) {
            int i4 = i + i2;
            int remaining = byteBuffer2.remaining();
            if (remaining <= 0) {
                return -1;
            }
            int i5 = remaining;
            int i6 = i;
            while (i6 < i4) {
                int i7 = i5 - 1;
                if (i5 <= 0) {
                    return i6 - i;
                }
                byte b = this._byteBuffer.get();
                if (b >= 0) {
                    i3 = i6 + 1;
                    cArr[i6] = (char) b;
                } else if (i6 >= i4 - 1) {
                    this._byteBuffer.position(byteBuffer.position() - 1);
                    return i6 - i;
                } else {
                    int read2 = read2(b);
                    i7 = this._byteBuffer.remaining();
                    if (read2 < 65536) {
                        i3 = i6 + 1;
                        cArr[i6] = (char) read2;
                    } else if (read2 > 1114111) {
                        throw new CharConversionException("Cannot convert U+" + Integer.toHexString(read2) + " to char (code greater than U+10FFFF)");
                    } else {
                        int i8 = i6 + 1;
                        int i9 = read2 - 65536;
                        cArr[i6] = (char) ((i9 >> 10) + 55296);
                        i3 = i8 + 1;
                        cArr[i8] = (char) ((i9 & RCommandClient.MAX_CLIENT_PORT) + Utf8.LOG_SURROGATE_HEADER);
                    }
                }
                i5 = i7;
                i6 = i3;
            }
            return i2;
        }
        throw new IOException("Reader closed");
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r0v14, types: [int] */
    /* JADX WARN: Type inference failed for: r0v5, types: [byte] */
    /* JADX WARN: Type inference failed for: r0v8, types: [int] */
    public void read(Appendable appendable) throws IOException {
        if (this._byteBuffer == null) {
            throw new IOException("Reader closed");
        }
        while (this._byteBuffer.hasRemaining()) {
            int i = this._byteBuffer.get();
            if (i < 0 && (i = read2(i)) >= 65536) {
                if (i > 1114111) {
                    throw new CharConversionException("Cannot convert U+" + Integer.toHexString(i) + " to char (code greater than U+10FFFF)");
                }
                int i2 = i - 65536;
                appendable.append((char) ((i2 >> 10) + 55296));
                i = (i2 & RCommandClient.MAX_CLIENT_PORT) + Utf8.LOG_SURROGATE_HEADER;
            }
            appendable.append((char) i);
        }
    }

    @Override // java.io.Reader
    public boolean ready() throws IOException {
        ByteBuffer byteBuffer = this._byteBuffer;
        if (byteBuffer != null) {
            return byteBuffer.hasRemaining();
        }
        throw new IOException("Reader closed");
    }

    @Override // java.io.Reader, javolution.lang.Reusable
    public void reset() {
        this._byteBuffer = null;
        this._code = 0;
        this._moreBytes = 0;
    }

    public UTF8ByteBufferReader setByteBuffer(ByteBuffer byteBuffer) {
        return setInput(byteBuffer);
    }

    public UTF8ByteBufferReader setInput(ByteBuffer byteBuffer) {
        if (this._byteBuffer == null) {
            this._byteBuffer = byteBuffer;
            return this;
        }
        throw new IllegalStateException("Reader not closed or reset");
    }
}
